import 'package:flutter/material.dart';

/// 用户信息页面
class UserInfoPage extends StatelessWidget {
  final Map<String, dynamic>? userInfo;
  final bool isLoading;
  final String? errorMessage;
  final VoidCallback onRefresh;
  
  const UserInfoPage({
    super.key,
    this.userInfo,
    required this.isLoading,
    this.errorMessage,
    required this.onRefresh,
  });
  
  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }
    
    if (errorMessage != null) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              Icons.error_outline,
              size: 64,
              color: Colors.red,
            ),
            const SizedBox(height: 16),
            Text(
              errorMessage!,
              style: const TextStyle(fontSize: 16, color: Colors.red),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 16),
            ElevatedButton.icon(
              onPressed: onRefresh,
              icon: const Icon(Icons.refresh),
              label: const Text('重试'),
            ),
          ],
        ),
      );
    }
    
    if (userInfo == null) {
      return const Center(
        child: Text('暂无用户信息'),
      );
    }
    
    return RefreshIndicator(
      onRefresh: () async => onRefresh(),
      child: SingleChildScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Row(
                  children: [
                    CircleAvatar(
                      radius: 40,
                      backgroundColor: Colors.blue.shade100,
                      child: userInfo!['avatar_url'] != null
                          ? ClipOval(
                              child: Image.network(
                                userInfo!['avatar_url'],
                                width: 80,
                                height: 80,
                                fit: BoxFit.cover,
                                errorBuilder: (context, error, stackTrace) {
                                  return const Icon(
                                    Icons.person,
                                    size: 40,
                                    color: Colors.blue,
                                  );
                                },
                              ),
                            )
                          : const Icon(
                              Icons.person,
                              size: 40,
                              color: Colors.blue,
                            ),
                    ),
                    const SizedBox(width: 16),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            userInfo!['name'] ?? userInfo!['login'] ?? '未知',
                            style: const TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          if (userInfo!['login'] != null) ...[
                            const SizedBox(height: 4),
                            Text(
                              '@${userInfo!['login']}',
                              style: TextStyle(
                                fontSize: 14,
                                color: Colors.grey.shade600,
                              ),
                            ),
                          ],
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      '详细信息',
                      style: TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Divider(),
                    _buildInfoRow('ID', userInfo!['id']?.toString() ?? '未知'),
                    _buildInfoRow('用户名', userInfo!['login'] ?? '未知'),
                    if (userInfo!['name'] != null)
                      _buildInfoRow('姓名', userInfo!['name']),
                    if (userInfo!['email'] != null)
                      _buildInfoRow('邮箱', userInfo!['email']),
                    if (userInfo!['bio'] != null)
                      _buildInfoRow('简介', userInfo!['bio']),
                    if (userInfo!['location'] != null)
                      _buildInfoRow('位置', userInfo!['location']),
                    if (userInfo!['company'] != null)
                      _buildInfoRow('公司', userInfo!['company']),
                    if (userInfo!['blog'] != null)
                      _buildInfoRow('博客', userInfo!['blog']),
                    if (userInfo!['public_repos'] != null)
                      _buildInfoRow(
                        '公开仓库',
                        userInfo!['public_repos'].toString(),
                      ),
                    if (userInfo!['followers'] != null)
                      _buildInfoRow(
                        '关注者',
                        userInfo!['followers'].toString(),
                      ),
                    if (userInfo!['following'] != null)
                      _buildInfoRow(
                        '关注中',
                        userInfo!['following'].toString(),
                      ),
                    if (userInfo!['created_at'] != null)
                      _buildInfoRow(
                        '注册时间',
                        _formatDate(userInfo!['created_at']),
                      ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
  
  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 100,
            child: Text(
              label,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                color: Colors.grey.shade700,
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: const TextStyle(fontSize: 14),
            ),
          ),
        ],
      ),
    );
  }
  
  String _formatDate(String? dateString) {
    if (dateString == null) return '未知';
    try {
      final date = DateTime.parse(dateString);
      return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
    } catch (e) {
      return dateString;
    }
  }
}

